<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_User extends Controller_Admin_Main
{

    private $user;

    public function before()
    {
        parent::before();
        $this->user = Model::factory('user');
        I18n::set_current_file('user');
    }

    //普通用户列表
    public function action_list()
    {
        $this->user_list(array('is_admin' => 0));
    }

    //管理员
    public function action_admin()
    {
        $this->user_list(array('is_admin' => 1));
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        $user_detail = $this->user->get_user_info_by_id($id);
        
        
        $this->template->assign('data',$user_detail);
        $this->template->view('user_edit');
        
    }

    //搜索
    public function action_search()
    {
        $data = array();
        if ($_POST['group_id'] != '')
        {
            $data['group_id'] = $_POST['group_id'];
        }

        if ($_POST['status'] != '')
        {
            $data['status'] = $_POST['status'];
        }

        if ($_POST['keywords'] != '')
        {
            $data['username'] = $_POST['keywords'];
        }
        $data['is_admin'] = 0;
        $this->user_list($data);
    }

    public function user_list($data)
    {
        //获取用户组列表生成select
        $select_group_list = Model::factory('user_group')->select_group_list();
        $this->template->assign('select_group_list', $select_group_list);
        $user = $this->user->get_user_list($data);
        $this->template->assign('user_list', $user['list']);
        $this->template->assign('page', $user['page']);
        $this->template->view('user_list');
    }

    //设置或者取消  管理员身份
    public function action_set_admin()
    {
        $id = $this->request->param('id');
        $current_status = $this->user->get_var(array('id' => $id), 'is_admin');
        $change_status = $current_status == 1 ? 0 : 1;
        if ($this->user->update(array('is_admin' => $change_status), array('id' => $id)))
        {
            Message::success(__('update success'));
            $this->request->redirect('admin/user/list');
        }
    }

    //修改用户的激活状态
    public function action_status()
    {
        $id = $this->request->param('id');
        $status = $this->request->param('status');

        if (!empty($id) && !empty($status))
        {
            if ($this->user->update(array('status' => $status), array('id' => $id)))
            {
                Message::success(__('update success'));
            }
        }
        $this->request->redirect('admin/user/list');
    }

    public function action_delete()
    {
        $id = intval($this->request->param('id'));
        if ($this->user->delete(array('id' => $id)))
        {
            Message::success(__('delete success'));
            $this->request->redirect('admin/user/list');
        }
    }

    public function action_left()
    {
        $left_arr[0]['name'] = '用户管理';
        $left_arr[0]['sub'] = array(
            array('name' => '用户列表', 'url' => URL::site() . 'admin/user/list', 'id' => 1201),
            array('name' => '管理员列表', 'url' => URL::site('admin/user/admin'), 'id' => 1204),
        );
        $left_arr[1]['name'] = '用户组管理';
        $left_arr[1]['sub'] = array(
            array('name' => '用户组', 'url' => URL::site() . 'admin/usergroup/list', 'id' => 1202),
            array('name' => '操作权限', 'url' => URL::site() . 'admin/privilege/list', 'id' => 1203),
        );

        $this->template->assign('left_arr', $left_arr);
        $this->template->view('left');
    }

}